<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT admin FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( isset( $row[ 'admin' ] ) && $row[ 'admin' ] == 1 ) {
?>
<html>
	<head>
		<title>
			Ages of Strife: Admin panel
		</title>
		<link type="text/css" rel="stylesheet" href="../global/agesofstrife.css" />
	</head>
	<body>
		<div id="root_container">
<?php
	include( "../global/ads.php" );
?>
			<div id="container">
				<div id="main-content">
					<div class="blok">
						<strong>Possible multies</strong>
					</div>
					<br />
<?php
	$allIps = Array();
	
	$statement = $dal->prepare( 'SELECT DISTINCT ip ' .
			'FROM access_history ' .
			'WHERE ip != \'\' ' .
			'ORDER BY timestamp DESC' );
	$ips = $dal->getArrayOfObjects( $statement );
	
	$statement = $dal->prepare( 'SELECT * ' .
			'FROM access_history ' .
			'WHERE ip = :ip AND timestamp > :minTime AND timestamp < :maxTime' );
	$ipAddress = '';
	$minTime = -1;
	$maxTime = -1;
	$statement->bindParam( ':ip', $ipAddress, PDO::PARAM_STR );
	$statement->bindParam( ':minTime', $minTime, PDO::PARAM_INT );
	$statement->bindParam( ':maxTime', $maxTime, PDO::PARAM_INT );
	
	$timeLimit = time() - 31536000;
	$statement2 = $dal->prepare( 'SELECT timestamp ' .
			'FROM access_history ' .
			'WHERE ip = :ip AND timestamp > :timeLimit ' .
			'ORDER BY timestamp DESC' );
	$statement2->bindParam( ':ip', $ipAddress, PDO::PARAM_STR );
	$statement2->bindParam( ':timeLimit', $timeLimit, PDO::PARAM_INT );
	
	foreach( $ips AS $ip ) {
		$ipAddress = $ip->ip;
		$checkUser = Array();
		$suspect = new stdClass();
		$suspect->ip = $ipAddress;
		$suspect->accounts = Array();
		$timestamps = $dal->getArrayOfObjects( $statement2 );
		foreach( $timestamps AS $timestamp ) {
			$minTime = $timestamp->timestamp - 3600;
			$maxTime = $timestamp->timestamp + 3600;
			$logins = $dal->getArrayOfObjects( $statement );

			$suspect->amount += count( $logins );
			foreach( $logins AS $login ) {
				if( !in_array( $login->user_id, $checkUser ) ) {
					$checkUser[] = $login->user_id;
					$account = new stdClass();
					$account->user_id = $login->user_id;
					$account->admin_id = $login->admin_id;
					$account->timestamp = $timestamp->timestamp;
					$suspect->accounts[] = $account;
				}
			}
		}
		$allIps[] = $suspect;
	}
	
	
	$suspectIps = Array();
	foreach( $allIps AS $ip ) {
		// Add only the ones with 2 accounts on an IP
		if( count( $ip->accounts ) > 1 ) {
			$suspectIps[] = $ip;
		}
	}
	
	$userId = -1;
	$statement = $dal->prepare( 'SELECT * ' .
			'FROM ruler ' .
			'WHERE user_id = :user_id' ); 
	$statement->bindParam( ':user_id', $userId, PDO::PARAM_INT );
	
	foreach( $suspectIps AS $suspectIp ) {
?>
					<div class="blok">
						<strong><?php print( $suspectIp->ip ); ?></strong><br />
						Number of logins: <?php print( number_format( $suspectIp->amount ) ); ?><br />
						<ol>
<?php
		foreach( $suspectIp->accounts AS $account ) {
			$userId = $account->user_id;
			$user = $dal->getObject( $statement );
?>
							<li><?php print( $user->name ); ?> - <?php print( date( 'G:i j F Y', $account->timestamp ) ); ?></li>
<?php
		}
?>						
						</ol>
					</div>
					<br />
<?php
		
	}
?>
					<div class="blok">
						<a href="admin.php">Back</a><br/>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
	<?php
}
else {
	$_SESSION[ 'admin' ] = 0;
	header( "Location: ../overview/overview.php" );
}
?>
